package com.hmyd.hmyd_android.ui;

import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.User;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.SmsShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@ContentView(R.layout.activity_invite_friend)
public class InviteFriendActivity extends BasicActivity {
	
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	
	@ViewInject(R.id.img_qr_code)
	public ImageView img_qr_code;
	@ViewInject(R.id.weixin)
	public RelativeLayout weixin;
	@ViewInject(R.id.pengyouquan)
	public RelativeLayout pengyouquan;
	@ViewInject(R.id.weibo)
	public RelativeLayout weibo;
	@ViewInject(R.id.QQfriend)
	public RelativeLayout QQfriend;
	@ViewInject(R.id.kongjian)
	public RelativeLayout kongjian;
	@ViewInject(R.id.duanxin)
	public RelativeLayout duanxin;
	
	private int QR_WIDTH = 200, QR_HEIGHT = 200;
	Bitmap bitmap;

	@OnClick({ R.id.weixin, R.id.pengyouquan, R.id.weibo, R.id.QQfriend, R.id.kongjian, R.id.duanxin })
	public void Click(View view){
		UMSocialService mController = UMServiceFactory.getUMSocialService("myshare");
		switch (view.getId()) {
		case R.id.weixin:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				UMWXHandler wxHandler = new UMWXHandler(InviteFriendActivity.this, "wx8982697092015f6c",// 正式key
						"d4624c36b6795d1d99dcf0547af5443d");
				// // 设置微信好友分享内容
				WeiXinShareContent weixinContent = new WeiXinShareContent();
				// 设置分享文字
				weixinContent.setShareContent("国泰·惠民益贷");
				// 设置title
				weixinContent.setTitle("国泰·惠民益贷");
				// 设置分享内容跳转URL
				weixinContent.setTargetUrl(AppBasic.app.QRcodeUrl+"&urlToken="+AppBasic.urlToken);
				//	设置分享图片
				weixinContent.setShareImage(new UMImage(InviteFriendActivity.this, bitmap));
				mController.setShareMedia(weixinContent);
				wxHandler.addToSocialSDK();
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.WEIXIN, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
//			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
//			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
//			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
//			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			break;
			
		case R.id.pengyouquan:	
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				UMWXHandler wxCircleHandler = new UMWXHandler(InviteFriendActivity.this, "wx8982697092015f6c",
						"d4624c36b6795d1d99dcf0547af5443d");
				// 设置微信朋友圈分享内容
				CircleShareContent circleMedia = new CircleShareContent();
				//	设置分享文字
				circleMedia.setShareContent("国泰·惠民益贷");
				// 设置朋友圈title
				circleMedia.setTitle("国泰·惠民益贷");
				//	设置分享图片
				circleMedia.setShareImage(new UMImage(InviteFriendActivity.this, bitmap));
				// 设置点击分享内容的跳转链接
				circleMedia.setTargetUrl(AppBasic.app.QRcodeUrl+"&urlToken="+AppBasic.urlToken);
				mController.setShareMedia(new UMImage(InviteFriendActivity.this, bitmap));
				wxCircleHandler.setToCircle(true);
				wxCircleHandler.addToSocialSDK();
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.WEIXIN_CIRCLE, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
//			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
//			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
//			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
//			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			break;
			
		case R.id.weibo:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				mController.getConfig().setSsoHandler(new SinaSsoHandler());
				SinaShareContent sinaContent = new SinaShareContent();
				// 设置点击分享内容的跳转链接
				sinaContent.setTargetUrl(AppBasic.app.QRcodeUrl+"&urlToken="+AppBasic.urlToken);
				//	设置分享内容的标题
				sinaContent.setTitle("国泰·惠民益贷");
				//	设置分享文字
				sinaContent.setShareContent("国泰·惠民益贷");
				//	设置分享图片
				sinaContent.setShareImage(new UMImage(InviteFriendActivity.this, bitmap));
				mController.setShareMedia(sinaContent);
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.SINA, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
//			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
//			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
//			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
//			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			
			break;
		case R.id.QQfriend:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				UMQQSsoHandler qqHandler = new UMQQSsoHandler(InviteFriendActivity.this, "1104617837", "yinRoSbFypECl3pE");
	
				QQShareContent qqShareContent = new QQShareContent();
				// 设置分享内容的标题
				qqShareContent.setTitle("国泰·惠民益贷");
				//	设置分享文字
				qqShareContent.setShareContent("国泰·惠民益贷");
				//	设置分享图片
				qqShareContent.setShareImage(new UMImage(InviteFriendActivity.this, bitmap));
				// 设置点击分享内容的跳转链接
				qqShareContent.setTargetUrl(AppBasic.app.QRcodeUrl+"&urlToken="+AppBasic.urlToken);
				mController.setShareMedia(new UMImage(InviteFriendActivity.this, bitmap));
				qqHandler.addToSocialSDK();
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.QQ, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
//			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
//			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
//			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
//			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			break;
		case R.id.kongjian:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				QZoneSsoHandler qzoneHandler = new QZoneSsoHandler(InviteFriendActivity.this, "1104617837",
						"yinRoSbFypECl3pE");
				QZoneShareContent qzone = new QZoneShareContent();
				// 设置分享内容的标题
				qzone.setTitle("国泰·惠民益贷");
				//	设置分享文字
				qzone.setShareContent("国泰·惠民益贷");
				//	设置分享图片
				qzone.setShareImage(new UMImage(InviteFriendActivity.this, bitmap));
				// 设置点击消息的跳转URL
				qzone.setTargetUrl(AppBasic.app.QRcodeUrl+"&urlToken="+AppBasic.urlToken);
				mController.setShareMedia(qzone);
				qzoneHandler.addToSocialSDK();
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.QZONE, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
//			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
//			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
//			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
//			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			break;
		/*case R.id.duanxin:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.QRcodeUrl)) {
				SmsHandler smsHandler = new SmsHandler();
				SmsShareContent smsShareContent = new SmsShareContent();
				smsShareContent.setShareContent("国泰·惠民益贷");
				smsHandler.addToSocialSDK();
			}
			mController.postShare(InviteFriendActivity.this,SHARE_MEDIA.SMS, 
			        new SnsPostListener() {
			                @Override
			                public void onStart() {
			                    Toast.makeText(InviteFriendActivity.this, "开始分享.", Toast.LENGTH_SHORT).show();
			                }
			                @Override
			                public void onComplete(SHARE_MEDIA platform, int eCode,SocializeEntity entity) {
			                     if (eCode == 200) {
			                         Toast.makeText(InviteFriendActivity.this, "分享成功.", Toast.LENGTH_SHORT).show();
			                     } else {
			                          String eMsg = "";
			                          if (eCode == -101){
			                              eMsg = "没有授权";
			                          }
			                          Toast.makeText(InviteFriendActivity.this, "分享失败[" + eCode + "] " + 
			                                             eMsg,Toast.LENGTH_SHORT).show();
			                     }
			              }
			});
			break;*/
		default:
			break;
		}
	}
	
	@Override
	public void onPostSuccess(int postId, String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void setParams() {//
		// TODO Auto-generated method stub
		home_title_txt.setText("邀请好友");
		home_right.setText("邀请说明");
		home_title.setOnTouchListener(touch);
		
		createImage();
	}

	User user = null;
	private void createImage() {
		try {
            // 需要引入core包
            QRCodeWriter writer = new QRCodeWriter();

            String text = AppBasic.app.QRcodeUrl
    				+"&urlToken="+AppBasic.urlToken;
            if (text == null || "".equals(text) || text.length() < 1) {
                return;
            }

            // 把输入的文本转为二维码
            BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
                    QR_WIDTH, QR_HEIGHT);

            System.out.println("w:" + martix.getWidth() + "h:"
                    + martix.getHeight());

            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                    BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }

                }
            }

            bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                    Bitmap.Config.ARGB_8888);

            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            img_qr_code.setImageBitmap(bitmap);

        } catch (WriterException e) {
            e.printStackTrace();
        }
		
    }
	
	@Override
	protected void leftButton() {
		// TODO Auto-generated method stub
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		// TODO Auto-generated method stub
		if (AppBasic.app != null && !TextUtils.isEmpty(AppBasic.app.referrerInstructionsLinkUrl)) {
			Utils.jumpURL(this, AppBasic.app.referrerInstructionsLinkUrl, "邀请说明", "", false);
		}
	}

}
